package com.wsz.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.commonutils.JwtUtils;
import com.wsz.commonutils.MD5;
import com.wsz.educenter.pojo.Member;
import com.wsz.educenter.pojo.vo.RegisterVo;
import com.wsz.educenter.service.MemberService;
import com.wsz.educenter.mapper.MemberMapper;
import com.wsz.servicebase.exceptionhandler.MyTestException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
* @author wangs
* @description 针对表【ucenter_member(会员表)】的数据库操作Service实现
* @createDate 2022-10-02 20:26:09
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member>
    implements MemberService{

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(Member member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new MyTestException(20001,"登录失败");
        }

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);

        Member member1 = baseMapper.selectOne(queryWrapper);


        if(member1 == null){
            throw new MyTestException(20001,"该账户不存在");
        }

        if(!MD5.encrypt(password).equals(member1.getPassword())){
            throw new MyTestException(20001,"密码错误");
        }

        if(member1.getIsDisabled() == 1){
            throw new MyTestException(20001,"账户已被封禁");
        }

        String jwtToken = JwtUtils.getJwtToken(member1.getId(), member1.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String password = registerVo.getPassword();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
           || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)){
            throw new MyTestException(20001,"注册失败");
        }

        String codes = redisTemplate.opsForValue().get(mobile);

        if(!codes.equals(code)){
            throw new MyTestException(20001,"注册码错误或已过期");
        }

        QueryWrapper<Member> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile",mobile);
        Integer i = baseMapper.selectCount(queryWrapper);

        if(i > 0){
            throw  new MyTestException(20001,"该手机号已被被注册");
        }

        Member m = new Member();
        m.setMobile(mobile);
        m.setNickname(nickname);
        m.setPassword(MD5.encrypt(password));
        m.setIsDisabled(0);
        m.setAvatar("https://online-education-wsz.oss-cn-hangzhou.aliyuncs.com/1.png");
        baseMapper.insert(m);
    }

    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);

        Member member = baseMapper.selectOne(queryWrapper);

        return member;
    }

    @Override
    public Integer countRegister(String day) {
       return baseMapper.countRegisterDay(day);
    }
}




